package com.lq.javawebTest.core;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 读取加载xml文件，为一个java的实例对象
 * 负责存储所有的配置信息
 *     //读取xml配置文件，并且将结果保存在dataSource、SERVLETS、FILTERS
 *     //当然你可以一个一个的去截串获取xml文件中的内容来赋值，但是太麻烦了，我们就是用流行的框架进行完成
 * 配置类：只要主动加载了这个类，就会加载所有的配置内容
 */
public class Configuration {

    public static final int PORT;
    public static final String BASE_URL;

    //存放数据源的信息
    private static final Map<String, String> DATA_SOURCE = new ConcurrentHashMap<>(8);

    //放置servlet对应的配置
    private static final Map<String, String> SERVLETS =  new ConcurrentHashMap<>(8);

    //放置过滤器
    private static final List<String> FILTERS = new ArrayList<>();



    //读取配置文件web.xml,将内容读取到DATA_SOURCE和SERVLETS中
    static {
        //获取文档对象
        URL url = Thread.currentThread().getContextClassLoader().getResource("web.xml");
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(url);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        //获取到根节点
        if(document != null) {
            Element rootElement = document.getRootElement();

            //获取到数据源相关信息
            Element dataSource = rootElement.element("hikari-data-source");
            for(Iterator<Element> iterator = dataSource.elementIterator(); iterator.hasNext();) {
                Element element = iterator.next();
                Attribute name = element.attribute("name");
                Attribute value = element.attribute("value");
                DATA_SOURCE.put(name.getStringValue(), value.getStringValue());
            }

            //获取到Servlet相关信息
            Element servlets = rootElement.element("servlets");
            for(Iterator<Element> iterator = servlets.elementIterator(); iterator.hasNext();) {
                Element element = iterator.next();
                Element name = element.element("url");
                Element value = element.element("servlet-class");
                SERVLETS.put(name.getStringValue(), value.getStringValue());
            }

            //获取到Filters相关信息
            Element filters = rootElement.element("filters");
            for(Iterator<Element> iterator = filters.elementIterator(); iterator.hasNext();) {
                Element element = iterator.next();
                FILTERS.add(element.getStringValue());
            }

            //给PORT和BASE_URL赋值
            Element service = rootElement.element("server");
            PORT = Integer.parseInt(service.element("port").getStringValue());
            BASE_URL = service.element("base-url").getStringValue();
        }else{
            PORT = 8080;
            BASE_URL = "http://localhost:8080";
        }

    }


    public static Map<String, String> getDataSource() {
        return DATA_SOURCE;
    }

    public static Map<String, String> getSERVLETS() {
        return SERVLETS;
    }

    public static List<String> getFilters(){
        return FILTERS;
    }
}
